package com.project.zsxd.server.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.zsxd.server.common.bean.Result;
import com.project.zsxd.server.common.exception.SystemException;
import com.project.zsxd.server.common.util.CommonUtil;
import com.project.zsxd.server.user.dto.UserDTO;
import com.project.zsxd.server.user.entity.User;
import com.project.zsxd.server.user.service.IUserService;
import com.project.zsxd.server.user.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  用户前端控制器
 * </p>
 *
 * @author kaler
 * @since 2020-05-12
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @RequestMapping("/listByPage")
    public Result listByPage(User user, Page<User> page) {
        IPage<User> resultPage = userService.page(page, new QueryWrapper<User>(user));
        return Result.success(resultPage);
    }

    @RequestMapping("/getByUserName")
    public Result getByUserName(String userName) {
        //查询数据
        User user = userService.getById(userName);
        //封装VO
        UserVO userVO = new UserVO();
        CommonUtil.copyBean(user, userVO);
        return Result.success(userVO);
    }

    @RequestMapping("/update")
    public Result update(User user) {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        currentUser.setNickName(user.getNickName());
        currentUser.setHeadIcon(user.getHeadIcon());
        currentUser.setGender(user.getGender());
        currentUser.setCountry(user.getCountry());
        currentUser.setProvince(user.getProvince());
        currentUser.setCity(user.getCity());
        currentUser.setUseLanguage(user.getUseLanguage());
        //查询数据
        userService.updateById(currentUser);
        return Result.success(null);
    }


}
